package com.cq.digitalemployee.user.controller;

import com.cq.digitalemployee.result.BaseResult;
import com.cq.digitalemployee.result.SuccessResult;
import com.cq.digitalemployee.user.entity.po.SysPermission;
import com.cq.digitalemployee.user.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

/**
 * @author zzz
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @GetMapping("getAll")
    @RequiresPermissions("permission:getAll")
    public BaseResult getAll() {
        BaseResult result = new SuccessResult();
        result.setData(permissionService.findAll());
        return result;
    }

    @GetMapping("get")
    public BaseResult get(String id) {
        BaseResult result = new SuccessResult();
        result.setData(permissionService.findById(id));
        return result;
    }

    @GetMapping("getAllByUser")
    public BaseResult getAllByUser(String userId){
        BaseResult result = new SuccessResult();
        result.setData(permissionService.findAllPermissionByUser(userId));
        return result;
    }

    @GetMapping("getByUser")
    public BaseResult getByUser(String userId) {
        BaseResult result = new SuccessResult();
        result.setData(permissionService.findByUser(userId));
        return result;
    }

    @GetMapping("getByRole")
    public BaseResult getByRole(String roleId) {
        BaseResult result = new SuccessResult();
        result.setData(permissionService.findByRole(roleId));
        return result;
    }

    @PostMapping("add")
    public BaseResult add(SysPermission sysPermission, HttpSession session) {
        BaseResult result = new SuccessResult();
        if (sysPermission.getId() == null || sysPermission.getId().isEmpty()) {
            sysPermission.setId(UUID.randomUUID().toString());
        }
        if (session.getAttribute("loginUserId") != null) {
            sysPermission.setCreateUser((String) session.getAttribute("loginUserId"));
        } else {
            sysPermission.setCreateUser("-1");
        }
        int insertResult = permissionService.add(sysPermission);
        result.setData(insertResult);
        return result;
    }

    @PostMapping("edit")
    public BaseResult edit(SysPermission sysPermission, HttpSession session) {
        BaseResult result = new SuccessResult();
        if (session.getAttribute("loginUserId") != null) {
            sysPermission.setUpdateUser((String) session.getAttribute("loginUserId"));
        } else {
            sysPermission.setUpdateUser("-1");
        }
        sysPermission.setUpdateTime(new Date());
        int updateResult = permissionService.edit(sysPermission);
        result.setData(updateResult);
        return result;
    }

    @GetMapping("delete")
    public BaseResult delete(String id) {
        BaseResult result = new SuccessResult();
        int updateResult = permissionService.delete(id);
        result.setData(updateResult);
        return result;
    }

    @PostMapping("grantByUser")
    public BaseResult grantByUser(String permissionId, String userId) {
        BaseResult result = new SuccessResult();
        int updateResult = permissionService.grantByUser(permissionId, userId);
        result.setData(updateResult);
        return result;
    }

    @PostMapping("grantByRole")
    public BaseResult grantByRole(String permissionId, String roleId) {
        BaseResult result = new SuccessResult();
        int updateResult = permissionService.grantByRole(permissionId, roleId);
        result.setData(updateResult);
        return result;
    }

}
